package com.fy.user.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fy.common.util.HttpUtils;
import com.fy.user.service.UserService;
import com.fy.user.utils.HttpClientUtils;
import com.fy.user.vo.SocialUser;
import com.fy.user.vo.UserLoginVo;
import com.fy.user.vo.UserRegisterVo;
import com.google.gson.Gson;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Override
    public Boolean register(UserRegisterVo vo) {
        String password = vo.getPassword();
        //注册堆密码加密
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode(vo.getPassword());
        //添加到数据库
        return true;

    }

    @Override
    public String login(UserLoginVo vo) {
        String loginacct = vo.getLoginacct();
        String password = vo.getPassword();
        //1、去数据库查询
        //判断用户是否存在
        if (1 == 0) {
            //不存在，登录失败
            return null;
        } else {
            //获取到数据库里的password
            String password1 = "1234";
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            //进行密码匹配
            boolean matches = passwordEncoder.matches(password, password1);
            if (matches) {
                //登录成功
                return "登录成功";
            }
        }
        return null;
    }

    @Override
    public Map<String, String> weiboLogin(SocialUser socialUser) throws Exception {
        //具有登录和注册逻辑
        //获取用户登录返回的唯一uid判断用户是否已注册
        String uid = socialUser.getUid();
        //1.通过uid在数据库查询是存在用户
        Object user = new Object();
        Map<String, String> userPojo = new HashMap<>();
        if (1 == 0) {
            //这个用户已经注册过
            //更新用户的访问令牌的时间和access_token
        } else {
            //2、没有查到当前社交用户对应的记录我们就需要注册一个

            //3、查询当前社交用户的社交账号信息

            //注册庄用户
            Map<String, String> query = new HashMap<>();
            query.put("access_token", socialUser.getAccess_token());
            query.put("uid", socialUser.getUid());
            HttpResponse response = HttpUtils.doGet("https://api.weibo.com", "/2/users/show.json", "get", new HashMap<String, String>(), query);
            //获取用户信息
            if (response.getStatusLine().getStatusCode() == 200) {
                String json = EntityUtils.toString(response.getEntity());
                JSONObject jsonObject = JSON.parseObject(json);
                //获取y过户名称
                String name = jsonObject.getString("name");
                //获取性别
                String gender = jsonObject.getString("gender");
                //获取头像地址
                String profileImageUrl = jsonObject.getString("profile_image_url");
                //然后设置到对象中，插入数据库
                userPojo.put("name", name);
                userPojo.put("gender", gender);
                userPojo.put("profileImageUrl", profileImageUrl);
            }
        }
        return userPojo;
    }

    @Override
    public String weixinLogin(String accessTokenInfo) {
        //从accessTokenInfo中获取出来两个值 access_token 和 oppenid
        //把accessTokenInfo字符串转换成map集合，根据map里面中的key取出相对应的value
        Gson gson = new Gson();
        HashMap accessMap = gson.fromJson(accessTokenInfo, HashMap.class);
        String accessToken = (String) accessMap.get("access_token");
        String openid = (String) accessMap.get("openid");

        //3、拿到access_token 和 oppenid，再去请求微信提供固定的API，获取到扫码人的信息
        // 查询数据库当前用用户是否曾经使用过微信登录


        Object user = new Object();
        if (user == null) {
            System.out.println("新用户注册");
            //访问微信的资源服务器，获取用户信息
            String baseUserInfoUrl = "https://api.weixin.qq.com/sns/userinfo" +
                    "?access_token=%s" +
                    "&openid=%s";
            String userInfoUrl = String.format(baseUserInfoUrl, accessToken, openid);
            //发送请求
            String resultUserInfo = null;
            try {
                resultUserInfo = HttpClientUtils.get(userInfoUrl);
                System.out.println("resultUserInfo==========" + resultUserInfo);
            } catch (Exception e) {
                e.printStackTrace();
            }

            //解析json
            HashMap userInfoMap = gson.fromJson(resultUserInfo, HashMap.class);
            String nickName = (String) userInfoMap.get("nickname");      //昵称
            Double sex = (Double) userInfoMap.get("sex");        //性别
            String headimgurl = (String) userInfoMap.get("headimgurl");      //微信头像

            //把扫码人的个人信息添加到数据库中
        }
        return null;
    }
}
